package com.yumis.service.modules.shop.service.Impl;

import com.yumis.service.common.enums.HttpState;
import com.yumis.service.common.exception.GlobalException;
import com.yumis.service.common.vo.Response;
import com.yumis.service.model.pojo.CategoryService;
import com.yumis.service.model.pojo.Shop;
import com.yumis.service.modules.shop.mapper.ShopMapper;
import com.yumis.service.modules.shop.service.ShopService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@Service
public class ShopServiceImpl implements ShopService {
    @Resource
    private ShopMapper shopMapper;
    @Override
    public void addShop(Shop shop) {
        int currTime = (int) (System.currentTimeMillis()/1000);
        shop.setCreateTime(currTime);
        shop.setUpdateTime(currTime);
        int i=shopMapper.insert(shop);
        if (i!=1){
            throw new GlobalException(Response.buildResponse(HttpState.CREATED_FAILED));
        }
    }

    @Override
    public void deleteShopById(Long id) {
        int i=shopMapper.deleteByPrimaryKey(id);
        if (i !=1){
            throw new GlobalException(Response.buildResponse(HttpState.DELETE_ERROR));
        }
    }

    @Override
    public Shop queryShopById(Long id) {
        Shop shop=shopMapper.selectByPrimaryKey(id);
        if (shop ==null || shop.equals("") ){
            throw new GlobalException(Response.buildResponse(HttpState.DATA_NOT_FOUNT));
        }
        return shop;
    }

    @Override
    public void updateShop(Shop shop) {
        shop.setUpdateTime((int) (System.currentTimeMillis()/1000));
        int i=shopMapper.updateByPrimaryKeySelective(shop);
        if (i !=1){
            throw new GlobalException(Response.buildResponse(HttpState.UPDATE_ERROR));
        }
    }
}
